<?php

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class Login extends Model
{
    protected $table='user';
    protected $guarded=[];

    /*
     * 检验登录
     * */
    public static function check($request){
        $username=$request->input('username');
        $password=$request->input('password');
        $check_username=Login::where('username',"$username")->first();
        if(is_null($check_username)){
            $request->session()->flash('username_check_msg', '用户名不存在');
            $request->session()->flash('username_back', "$username");
            $request->session()->flash('password_back', "$password");
            return redirect('login');
        }else{
            if($check_username->status==1){
                $hashPassword=$check_username->password;
                if(Hash::check($password,$hashPassword)){
                    $admin_id=$check_username->id;
                    $admin_username=$check_username->username;
                    $safeKey="$admin_id"."$admin_username";
                    session(['safeKey'=>"$safeKey"]);
                    session(['admin_id'=>"$admin_id"]);
                    session(['admin_username'=>"$admin_username"]);
                    return redirect('index');

                }else{
                    $request->session()->flash('password_check_msg', '密码错误');
                    $request->session()->flash('username_back', "$username");
                    $request->session()->flash('password_back', "$password");
                    return redirect('login');
                }
            }else{
                $request->session()->flash('admin_forbid', '抱歉 ！该会员被禁用');
                $request->session()->flash('username_back', "$username");
                $request->session()->flash('password_back', "$password");
                return redirect('login');
            }


        }
    }
}
